/*
 * MsqBindParam object defines the needed conversion of bind parameters
 * Copyright (C) 2003 Johnathan Ingram, jingram@rogueware.org
 *
 * This library is free software; you can redistribute it and/or
 *   modify it under the terms of the GNU Lesser General Public
 *   License as published by the Free Software Foundation; either
 *   version 2.1 of the License, or (at your option) any later version.
 *
 *   This library is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *   Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public
 *   License along with this library; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  US
 *
 */


#include "msqlBindParam.h"
#include "dbconnectTypes.h"


// -----------------------------------------------------------------------------
// MsqlBindParam::paramToMSQLString
// -----------------------------------------------------------------------------
string 
MsqlBindParam::paramToMSQLString()
{  
   // Check if the value is null and return the appropriate null string
   if (!valuePtr.ptr)
      return "NULL";
   
   string res;
   char* to;
   
   switch(type)
   {
      case BP_STRING:
         // TODO: Find the correct way to escape the string and implement it
         res = "'";
         res += valuePtr.charPtr;       
         res += "'";
         break;      

      case BP_BINARY:                   
         // Binary is not supported
         throw BindParameterError("paramToMSQLString(): The mSQL database does not support binary data.");   
         break;      
     
      case BP_LONG:
         to = (char*)malloc(512);
         sprintf(to, DBLONG_SPRINTF, *valuePtr.longPtr);
         res = to;       
         free(to);         
         break;      

      case BP_ULONG:
         to = (char*)malloc(512);
         sprintf(to, DBULONG_SPRINTF, *valuePtr.unsignedLongPtr);
         res = to;       
         free(to);         
         break;      

      case BP_DOUBLE:
         to = (char*)malloc(512);
         sprintf(to, "%.25E", *valuePtr.doublePtr);    // Use a precision of 25 for the conversion
         res = "'";
         res += to;       
         res += "'";
         free(to);         
         break;      
      
      case BP_DATETIME:        
         // DateTime is not supported
         throw BindParameterError("paramToMSQLString(): The mSQL database does not support datetime types, only date and time.");            
         break;      

      case BP_DATE:
         // 'dd-mmm-yyy' string format
         res = "'";
         res += valuePtr.JDatePtr->asString("%d-%b-%Y");
         res += "'";
         break;      
           
      case BP_TIME:
         // 'hh:mm:ss' string format
         res = "'";
         res += valuePtr.JDatePtr->asString("%H:%M:%S");
         res += "'";
         break;      

      case BP_BOOLEAN:
         // Convert a boolean to an integer value.
         if (*valuePtr.boolPtr)
            res = "1";
         else
            res = "0";
         break;
   }

   return res;
}  // MsqlBindParam::paramToMSQLString
